//
//  CourseDetailViewController.m
//  TMS
//
//  Created by Riddhi Joshi on 23/01/15.
//  Copyright (c) 2015 Riddhi Joshi. All rights reserved.
//

#import "CourseDetailViewController.h"
#import "DBManager.h"

@interface CourseDetailViewController ()

@property (nonatomic, strong) DBManager *dbManager;
@property (nonatomic, strong) NSArray *arrCourseInfo;
@property (nonatomic, strong) UITextField *reason;

-(void)loadInfoToDisplay;


@end

@implementation CourseDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.DescriptionTextView.userInteractionEnabled=YES;
    self.DescriptionTextView.editable=NO;
    
    // self.dbManager = [[DBManager alloc] initWithDatabaseFilename:@"tmsdb.sql"];
    
    NSLog(@"Record id in Course detailview controller =%d",self.recordIDToDisplay);
    NSLog(@"Trainee id in Course detailview controller=%d",self.TraineeId);
    
    if(self.recordIDToDisplay!=-1)
    {
        [self loadInfoToDisplay];
    }
    
    
    //1 for accepted 2 for rejected
    if (self.StatusId==2) {
        [self.Reject setEnabled:NO];
    }
    
    
    if (self.StatusId==1) {
        [self.Accept setEnabled:NO];
    }
}

-(void) viewWillAppear:(BOOL)animated{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

-(void) loadInfoToDisplay
{

    
    // Load the relevant data.
    
    if (self.arrCourseInfo != nil) {
        self.arrCourseInfo = nil;
    }
    
    self.CourseTable=[[CourseTable alloc]init];
    self.arrCourseInfo=[[NSArray alloc]init];
    self.arrCourseInfo=[self.CourseTable LoadCourseWithCourseID:self.recordIDToDisplay];
    
    // Set the loaded data to the textfields.
    
    Course *course=[self.arrCourseInfo objectAtIndex:0];
    self.CourseName.text=course.cname;
    self.CoursePlatform.text=course.platform;
    self.DescriptionTextView.text=course.cdesc;
    
}


- (IBAction)onAcceptClicked:(id)sender {
    
    NSDate *date=[NSDate date];
    NSDateFormatter *dateformat=[[NSDateFormatter alloc]init];
    [dateformat setDateFormat:@"dd/MM/yyyy"];
    NSString *dateInString=[dateformat stringFromDate:date];
    
    NSLog(@"Current date %@",dateInString);
    
    self.TraineeCourseTable=[[TraineeCourseTable alloc]init];
    
    int accepted=1;
    
    
    NSArray *existingCourse=[[NSArray alloc]init];
    existingCourse=[self.TraineeCourseTable onAcceptCheckExistingCourseWithTid:self.TraineeId andStatus:accepted andDate:dateInString];
    
    
    if (existingCourse.count>0)
    {
    
        NSString *coursename=[[existingCourse objectAtIndex:0] objectAtIndex:[self.CourseTable.db.arrColumnNames indexOfObject:@"coursename"]];
        NSMutableString *showMessage=[[NSMutableString alloc] initWithString:coursename];
        [showMessage appendString:@" is already accepted"];
      
        UIAlertView * alert =[[UIAlertView alloc ] initWithTitle:@"Not Allowed" message:showMessage delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles: nil];
        alert.alertViewStyle = UIAlertViewStyleDefault;
        [alert addButtonWithTitle:@"Ok"];
        [alert setTag:1];
        [alert show];
    }
    else
    {
    
        int value;
        NSString *noreason=@"null";
        value=[self.TraineeCourseTable onAcceptwithCourseId:self.recordIDToDisplay andTraineeID:self.TraineeId andStatus:accepted andReason:noreason];
    
        if(value==1)
        {
        // Inform the delegate that the approving was finished.
        [self.delegate approvingCourseInfoWasFinished];
            
        // Pop the view controller.
        [self.navigationController popViewControllerAnimated:YES];
        }
    
        else
        {
            NSLog(@"Could not execute the query.");
        }
        
    }
 }

- (IBAction)onRejectClicked:(id)sender {
    
    UIAlertView * alert =[[UIAlertView alloc ] initWithTitle:@"Rejected" message:@"Enter your reason" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles: nil];
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    [alert addButtonWithTitle:@"Ok"];
    [alert setTag:2];
    [alert show];
    
    
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    //alertview tag 2 for reject
    if(alertView.tag==2)
    {
    if (buttonIndex == alertView.cancelButtonIndex)
    {
        // cancel clicked ...do your action
        [self.delegate approvingCourseInfoWasFinished];
        
        // Pop the view controller.
        [self.navigationController popViewControllerAnimated:YES];
    }
    else if (buttonIndex == 1)
    {
        // 1st button  clicked ...do your action
        _reason=[alertView textFieldAtIndex:0]; //ok
        NSLog(@"reason: %@", _reason.text);
        
        
        self.TraineeCourseTable=[[TraineeCourseTable alloc]init];
        int rejected=2;
        int value;
        
        value=[self.TraineeCourseTable onRejectwithCourseId:self.recordIDToDisplay andTraineeID:self.TraineeId andStatus:rejected andReason:_reason.text];

        if(value==1)
        {
            // Inform the delegate that the approving was finished.
            [self.delegate approvingCourseInfoWasFinished];
            
            
            // Pop the view controller.
            [self.navigationController popViewControllerAnimated:YES];
        }

        else{
            NSLog(@"Could not execute the query.");
        }

    }
    }
    else if(alertView.tag==1) //  alertview tag 1 for accept 
    {
        
        if (buttonIndex == alertView.cancelButtonIndex)
        {
            // cancel clicked ...do your action
            [self.delegate approvingCourseInfoWasFinished];
            
            // Pop the view controller.
            [self.navigationController popViewControllerAnimated:YES];
        }
        else if (buttonIndex == 1)
        {
            // 1st button  clicked ...do your action
            
            // Inform the delegate that the approving was finished.
            [self.delegate approvingCourseInfoWasFinished];
            
            
            // Pop the view controller.
            [self.navigationController popViewControllerAnimated:YES];
                  }

        
    }
 
}
@end
